const http = require('node:http')
const url = require('node:url')
const fs = require('node:fs')
const config = require('./config.js')
const { createProxyMiddleware } = require('http-proxy-middleware')
const html = fs.readFileSync('./index.html')

http.createServer((req, res) => {
    const { pathname } = url.parse(req.url)

    const proxyList = Object.keys(config.serve.proxy)
    console.log('pathname', pathname, proxyList)
    const target = '/' + pathname.split('/')[1]
    if (proxyList.includes(target)) {
        const proxy = createProxyMiddleware(config.serve.proxy[target])
        proxy(req, res)
        return
    }
    res.writeHead(200, {
        'content-type': 'text/html;chaset=utf-8'
    })
    res.end(html)
}).listen(80, () => {
    console.log('80 端口启动了')
})